package com.gxa.service.community.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.communitydto.HouseOwnerDto;
import com.gxa.entity.community.House;
import com.gxa.entity.community.Owner;
import com.gxa.mapper.community.HouseMapper;
import com.gxa.service.community.HouseService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public PageInfo queryAll(House house, Integer pageNum, Integer pageSize) {
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isNullOrEmpty(house.getHouseNum()) || !"".equals(house.getHouseNum()), "house_num", house.getHouseNum())
                .like(!StringUtils.isNullOrEmpty(house.getHouseName()) || !"".equals(house.getHouseName()), "house_name", house.getHouseName())
                .eq(  null != house.getHouseType(), "house_type", house.getHouseType())
                .like( !StringUtils.isNullOrEmpty(house.getHouseAddress()) || !"".equals(house.getHouseAddress()), "house_address", house.getHouseAddress())
                .like( !StringUtils.isNullOrEmpty(house.getHouseOwner()) || !"".equals(house.getHouseOwner()), "house_owner", house.getHouseOwner())
                .eq(null != house.getState(), "state", house.getState());
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<House> houses = houseMapper.selectList(wrapper);
        PageInfo<House> housePageInfo = new PageInfo<>(houses);
        return housePageInfo;
    }

    @Override
    public House getById(Integer id) {
        House house = houseMapper.selectById(id);
        return house;
    }

    @Override
    public List<HouseOwnerDto> queryByHouseId(String houseNum) {
        List<HouseOwnerDto> houseOwnerDtos = houseMapper.queryByHouseId(houseNum);
        return houseOwnerDtos;
    }

    @Override
    public void add(House house) {
        houseMapper.insert(house);
    }

    @Override
    public void deleteById(Integer id) {
        houseMapper.deleteById(id);
    }

    @Override
    public void update(House house) {
        houseMapper.updateById(house);
    }

    @Override
    public List<House> queryByNum(String houseNum) {
        List<House> houses = houseMapper.queryByNum(houseNum);
        return houses;
    }
}
